<template>
  <div>
    <!-- 面包屑视图区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 添加分类按钮区域 -->
      <el-row>
        <el-col>
          <el-button
            type="primary"
            @click="setAddCateDialogVisiable"
          >添加分类</el-button>
        </el-col>
      </el-row>
      <!-- tree-table 表格区域 -->
      <tree-table
        class="treeTable"
        :data="cateList"
        :columns="columns"
        :show-row-hover="false"
        index-text="编号"
        :show-index="true"
        :selection-type="false"
        :expand-type="false"
        border
      >
        <!-- 这是 是否有效 的模板 -->
        <template
          slot="isok"
          slot-scope="scope"
        >
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i
            class="el-icon-error"
            v-else
            style="color: red"
          ></i>
        </template>
        <!-- 这是 排序 的模板 -->
        <template
          slot="sort"
          slot-scope="scope"
        >
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag
            type="success"
            v-else-if="scope.row.cat_level === 1"
          >二级</el-tag>
          <el-tag
            type="warning"
            v-else
          >三级</el-tag>
        </template>
        <!-- 这是 操作 的模板 -->
        <template slot="operation">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
          >编辑</el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </tree-table>
      <!-- 导航栏区域 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类的 Dialog -->
    <el-dialog
      title="添加分类"
      :visible.sync="addSorts"
      width="50%"
      center
      @close="addSortsClose"
    >
      <el-form
        :model="addCateForm"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item
          label="分类名称"
          prop="cat_name"
          :rules="rules.cat_name"
        >
          <el-input
            type="text"
            v-model.number="addCateForm.cat_name"
          ></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!-- options指定数据源 -->
          <!-- props用来指定配置对象 -->
          <!-- v-model绑定的是选中的id（数组） -->
          <el-cascader
            expand-trigger="hover"
            v-model="seletedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChanged"
            clearable
          ></el-cascader>

        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addSorts=false">取 消</el-button>
        <el-button
          type="primary"
          @click="addCate"
        >提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      cateList: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      addCateForm: {
        cat_name: '',
        // 父id默认为0
        cat_pid: 0,
        cat_level: 0
      },
      // 父级分类列表
      parentCateList: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      seletedKeys: [],
      columns: [
        // 每个对象 代表 每一列
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', prop: 'cat_deleted', type: 'template', template: 'isok' },
        { label: '排序', prop: 'cat_level', type: 'template', template: 'sort' },
        { label: '操作', prop: 'cat_level', type: 'template', template: 'operation' }
      ],
      rules: {
        cat_name: [
          { required: true, message: '分类名称不能为空' }
        ]
      },
      total: 0,
      addSorts: false,
      addCateId: ''
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        this.$message.error('获取商品分类数据失败!')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 监听 页码值 的改变
    handleSizeChange (newPage) {
      this.queryInfo.pagesize = newPage
      this.getGoodsList()
    },
    // 监听 当前页码 的改变
    handleCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    addSortsClose () { },
    // 添加分类按钮
    async setAddCateDialogVisiable () {
      // 打开时，从服务器获取到商品分类数据列表
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) {
        this.$message.error('获取商品分类数据列表失败!')
      }
      this.parentCateList = res.data
      this.addSorts = true
    },
    parentCateChanged () {
      // 如果seletedKeys 数组中的length>0，证明选中了父级id
      if (this.seletedKeys.length > 0) {
        this.addCateForm.cat_pid = this.seletedKeys[this.seletedKeys.length - 1]
        // 为当前分类等级赋值
        this.addCateForm.cat_level = this.seletedKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    // 点击按钮 添加新的分类
    addCate () {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请输入完整信息!')
        }
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败!')
        }
        this.$message.success('添加分类成功!')
        this.getGoodsList()
        this.addCateForm.cat_name = ''
        this.addSorts = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-breadcrumb {
  padding: 15px;
}
.treeTable {
  margin-top: 15px;
}
.pagination {
  margin-top: 15px;
  text-align: center;
}
</style>
